package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.smallgame.SmallGameConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SmallGameConfigDaoImpl extends TempDao {
    private static String getSmallGameConfigSql = "select * from `t_p_smallgame`";
    public Map<Integer, Map<Integer, SmallGameConfig>> getSmallGameConfigList() {
        Map<Integer, Map<Integer, SmallGameConfig>> stageConfigMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getSmallGameConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                SmallGameConfig config = new SmallGameConfig();
                config.setId(rs.getInt("id"));
                config.setType(rs.getInt("type"));
                config.setLevel(rs.getInt("level"));
                config.setReward(rs.getString("reward"));
                config.setParam(rs.getString("param"));
                config.setStageData(rs.getString("stageData"));
                config.setUnlock(rs.getInt("unlock"));
                if (stageConfigMap.containsKey(config.getType())) {
                    stageConfigMap.get(config.getType()).put(config.getId(), config);
                } else {
                    Map<Integer, SmallGameConfig> mapConfig = new HashMap<>();
                    mapConfig.put(config.getId(), config);
                    stageConfigMap.put(config.getType(), mapConfig);
                }
            }
            return stageConfigMap;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }
}
